package com.credit.mola.app.utils.helper

import com.credit.mola.app.BuildConfig
import com.credit.mola.app.utils.config.Configs
import org.json.JSONObject

/**
 * 自定义客服配置
 */
object CustomConfigHelper {
    /**
     * 自定义数据来源
     *     1：客服配置
     *
     *      "customData": {
     *         "noticeInfo": {
     *             "num": 3,
     *             "isShow": "YES",
     *             "noticeTitle": "Aviso",
     *             "noticeText": "Por favor, não transfira os reembolsos para nenhuma conta privada para evitar fraudes e perdas!"
     *         },
     *         "release":{
     *              "isMustUpdate":true,
     *              "versionCode":"1",
     *              "versionName":"1.0.0",
     *              "versionRemark":""//更新描述 需用"$"分割
     *         }
     *         "workDate": "Monday to Sunday",
     *         "workTime": "9:30am to 19:30pm.",
     *     },
     */

    /**
     * 保存客服配置信息
     */
    fun saveCustomConfigData(data: String){
        Configs.saveSPString(Configs.SPKEY.SIKADWA_COMMON, data)
    }

    /**
     * 获取客服配置
     */
    fun getCmConfigData(): JSONObject? {
        val dataString = Configs.getSPString(Configs.SPKEY.SIKADWA_COMMON)
        if (dataString.isEmpty()){
            return null
        }
        val data = JSONObject(dataString)
        if (data.has("customize")){
            return data.optJSONObject("customize")
        }
        return null
    }

    /**
     * 获取客服配置里自定义数据
     * customData
     */
    fun getCmCustomData(): JSONObject?{
        val customize = getCmConfigData()
        if (customize != null) {
            return customize.optJSONObject("customData")
        }
        return null
    }

    /**
     * 客服服务日期
     */
    fun getCmCustomerWorkWeek(): String{
        val customData = getCmCustomData() ?: return ""
        return customData.optString("workDate")
    }

    /**
     * 客服服务时间
     */
    fun getCmCustomerWorkTime(): String{
        val customData = getCmCustomData() ?: return ""
        return customData.optString("workTime")
    }

    /**
     * 获取是否支持不传证件照
     */
    fun getCmOcrNoDocuments(): Boolean{
        val customData = getCmCustomData() ?: return false
        return customData.optString("ocrNoDocuments","") == "YES"
    }

    /**
     * 审核中、放款中、待还款是否显示公告信息
     */
    fun isCmShowNotice(): Boolean{
        val customData = getCmCustomData() ?: return false
        if (customData.has("noticeInfo")){
            val noticeInfo = customData.optJSONObject("noticeInfo") ?: return false
            return noticeInfo.optString("isShow","NO") == "YES"
        }
        return false
    }

    /**
     * 获取 审核中、放款中 显示的公告信息
     */
    fun getCmNoticeInfo(): JSONObject?{
        val customData = getCmCustomData() ?: return null

        var noticeData: JSONObject? = null
        if (customData.has("noticeInfo")){
            //默认为英语
            noticeData = customData.optJSONObject("noticeInfo")
        }
        return noticeData
    }

    /**
     * 获取版本更新信息
     */
    fun getCmVersionObject(): JSONObject?{
        val customData = getCmCustomData() ?: return null
        if (!customData.has("release")){
            return null
        }
        return customData.optJSONObject("release")
    }

    /**
     * 获取是否有强制更新（启动页使用）
     *  "isMustUpdate":true,
     *  "versionCode":"1",
     *  "versionName":"1.0.0",
     *  "versionRemark":"1xxx$2xxx$3xxx"
     */
    fun isCmVersionUpdateMust(): Boolean {
        val versionObj = getCmVersionObject() ?: return false
        val versionCode = versionObj.optInt("versionCode",1)
        val isMustUpdate = versionObj.optBoolean("isMustUpdate",false)
        return BuildConfig.VERSION_CODE < versionCode && isMustUpdate
    }

    /**
     * 获取是否有更新（首页使用）
     */
    fun isCmVersionUpdate(): Boolean{
        val versionObj = getCmVersionObject() ?: return false
        val versionCode = versionObj.optInt("versionCode",1)
        return BuildConfig.VERSION_CODE < versionCode
    }



}